package com.jpcc.vw.onlineRadio.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import com.jpcc.vw.onlineRadio.model.bean.RecentAlbumRadioTrackBean;
import com.jpcc.vw.onlineRadio.model.bean.RecentAlbumRadioTrackOnlineBean;

import java.sql.SQLException;

/**
 * Created by liucxu on 2017/4/14.
 */

public class DatabaseHelperOrm extends OrmLiteSqliteOpenHelper {

    private static final String TABLE_NAME = "sqlite_radio.db";
    /**
     * recentDao ，每张表对于一个
     */
    private Dao<RecentAlbumRadioTrackBean, Integer> recentDao;
    private Dao<RecentAlbumRadioTrackOnlineBean, Integer> recentOnlineDao;
    private Dao<FavouriteAlbumDbBean, Integer> favDao;
    private Dao<DownloadTrackDbBean, Integer> downloadDao;
    private Dao<SearchHistoryDbBean, Integer> searchHistoryDao;
    private Dao<FavouriteAlbumDbBeanOnline, Integer> favDbBeanOnline;

    private DatabaseHelperOrm(Context context) {
        super(context, TABLE_NAME, null, 5);
    }

    @Override
    public void onCreate(SQLiteDatabase database,
                         ConnectionSource connectionSource) {
        try {
            TableUtils.createTable(connectionSource, RecentAlbumRadioTrackBean.class);
            TableUtils.createTable(connectionSource, RecentAlbumRadioTrackOnlineBean.class);
            TableUtils.createTable(connectionSource, FavouriteAlbumDbBean.class);
            TableUtils.createTable(connectionSource, DownloadTrackDbBean.class);
            TableUtils.createTable(connectionSource, SearchHistoryDbBean.class);
            TableUtils.createTable(connectionSource, FavouriteAlbumDbBeanOnline.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase database,
                          ConnectionSource connectionSource, int oldVersion, int newVersion) {


        try {
            TableUtils.dropTable(connectionSource, RecentAlbumRadioTrackBean.class, true);
            TableUtils.dropTable(connectionSource, RecentAlbumRadioTrackOnlineBean.class, true);
            TableUtils.dropTable(connectionSource, FavouriteAlbumDbBean.class, true);
            TableUtils.dropTable(connectionSource, DownloadTrackDbBean.class, true);
            TableUtils.dropTable(connectionSource, SearchHistoryDbBean.class, true);
            TableUtils.dropTable(connectionSource, FavouriteAlbumDbBeanOnline.class, true);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        onCreate(database, connectionSource);
    }

    private static DatabaseHelperOrm instance;

    /**
     * 单例获取该Helper
     *
     * @param context
     * @return
     */
    public static synchronized DatabaseHelperOrm getHelper(Context context) {
        if (instance == null) {
            synchronized (DatabaseHelperOrm.class) {
                if (instance == null)
                    instance = new DatabaseHelperOrm(context);
            }
        }

        return instance;
    }

    /**
     * 获得最近收听的Dao
     *
     * @return
     * @throws
     */
    public Dao<RecentAlbumRadioTrackBean, Integer> getRecentDao() throws SQLException {
        if (recentDao == null) {
            recentDao = getDao(RecentAlbumRadioTrackBean.class);
        }
        return recentDao;
    }



    public Dao<RecentAlbumRadioTrackOnlineBean, Integer> getRecentOnlineDao() throws SQLException {
        if (recentOnlineDao == null) {
            recentOnlineDao = getDao(RecentAlbumRadioTrackOnlineBean.class);
        }
        return recentOnlineDao;
    }


    /**
     * 获取保存数据库中收藏的dao
     *
     * @return
     * @throws SQLException
     */
    public Dao<FavouriteAlbumDbBean, Integer> getFavouriteDao() throws SQLException {
        if (favDao == null) {
            favDao = getDao(FavouriteAlbumDbBean.class);
        }
        return favDao;
    }


    /**
     * 获取保存数据库中下载的dao
     *
     * @return
     * @throws SQLException
     */
    public Dao<DownloadTrackDbBean, Integer> getDownloadDao() throws SQLException {
        if (downloadDao == null) {
            downloadDao = getDao(DownloadTrackDbBean.class);
        }
        return downloadDao;
    }


    /**
     * 获取搜索历史相关的数据库dao
     * @return
     * @throws SQLException
     */
    public Dao<SearchHistoryDbBean, Integer> getSearchHistoryDao() throws SQLException {
        if (searchHistoryDao == null) {
            searchHistoryDao = getDao(SearchHistoryDbBean.class);
        }
        return searchHistoryDao;
    }



    /**
     * 获取收藏相关的
     * @return
     * @throws SQLException
     */
    public Dao<FavouriteAlbumDbBeanOnline, Integer> getOnlineFavouriteDao() throws SQLException {
        if (favDbBeanOnline == null) {
            favDbBeanOnline = getDao(FavouriteAlbumDbBeanOnline.class);
        }
        return favDbBeanOnline;
    }


    /**
     * 释放资源
     */
    @Override
    public void close() {
        super.close();
        recentDao = null;
    }
}
